var getColumn = []
var create
var showColumn

var endPage = document.createElement('div')
endPage.className = 'end'
var endMsg = document.createElement('div')
endMsg.className = 'msg'
var playAgain = document.createElement('button')
playAgain.className = 'playBtn'
playAgain.innerText = '再玩一次'
var backToIndex = document.createElement('a')
backToIndex.className = 'backA'
backToIndex.innerText = '返回首页'
backToIndex.href = 'index.html'

function gameEnd() {
	clearInterval(showColumn)
	clearInterval(drop)
	clearInterval(create)
	bg.appendChild(endPage)
	endPage.appendChild(endMsg)
	endPage.appendChild(playAgain)
	endMsg.innerHTML = '<h1>游 戏 结 束</h1>' + '<br>' + '本次得分:' + parseInt(score)
	endPage.appendChild(backToIndex)
	
	playAgain.onclick = function() {
		location.reload()
	}
}

function createColumn() {
	//产生柱子
	create = setInterval(function() {
		var newTopColumn = document.createElement('div') //新div,上面的管子
		var newBottomColumn = document.createElement('div') //下面的管子
		newTopColumn.className = 'column'
		newTopColumn.style.left = 1000 + 'px'
		newTopColumn.style.top = -Math.round(Math.random() * 200) + 'px'
		newTopColumn.id = new Date().getTime() //时间戳
		bg.appendChild(newTopColumn)
		newBottomColumn.className = 'column'
		newBottomColumn.style.left = 1000 + 'px'
		newBottomColumn.style.bottom = -Math.round(Math.random() * 200) + 'px'
		newBottomColumn.id = new Date().getTime() //时间戳
		bg.appendChild(newBottomColumn)
		getColumn.push(newTopColumn)
		getColumn.push(newBottomColumn)
	}, 400)
}

createColumn()
showColumn = setInterval(function() {
	for (var i = 0; i < getColumn.length; i++) {
		getColumn[i].style.left = (parseInt(getColumn[i].style.left) - 10) + 'px'
		if (parseInt(getColumn[i].style.left) - 40 < 200 && parseInt(getColumn[i].style.left) > 170) {
			if (parseInt(birdDiv.style.marginTop) < parseInt(getColumn[i].style.top) + 230 && i % 2 == 0) {
				//碰到上面管子
				gameEnd()
			} else if (parseInt(birdDiv.style.marginTop) > 500 - (parseInt(getColumn[i].style.bottom) + 230) && i % 2 == 1) {
				//碰到下面管子
				gameEnd()
			}
		} else {
			score += 0.1
			scoreText.innerText = '得分:' + parseInt(score)
		}
	}
}, 20)
